package com.wawei.seeds.services;

import android.os.Bundle;
import android.os.Handler;

import com.wawei.seeds.SeedsApp;
import com.wawei.seeds.model.HeroInfo;
import com.wawei.seeds.services.remote.HeroInfoRestfulWrapper;
import com.wawei.seeds.services.remote.WalnutRegAuthRestfulService;
import com.wawei.seeds.utils.NetBase;
import com.wawei.seeds.utils.RetrofitNetworkService;

import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;
import retrofit.mime.TypedFile;

/**
 *
 * Created by t27891 on 15/7/5.
 */
public class WalnutRegAuthServiceHandler implements ServiceHandler {
    @Override
    public void retrieveData(RestAdapter restAdapter, Bundle bundle, final Handler handler, final Integer handlerTag) {
        final HeroInfo heroInfoOut = (HeroInfo)bundle.getSerializable(SeedsApp.LOGIN_USER);
        final String clientId = (String)bundle.getString(SeedsApp.CLIENT_ID);
        WalnutRegAuthRestfulService service = restAdapter.create(WalnutRegAuthRestfulService.class);
        service.login(clientId, heroInfoOut.userName, heroInfoOut.password, new Callback<HeroInfoRestfulWrapper>() {
            @Override
            public void success(HeroInfoRestfulWrapper heroInfo, Response response) {
                HeroInfo hf = heroInfo.getUser();
                if(RetrofitNetworkService.FAILED == heroInfo.getResultCode()){
                    RetrofitNetworkService.handleError(handler, handlerTag, null);
                }
                else {
                    hf.password = heroInfoOut.password;
                    RetrofitNetworkService.handleSuccess(handler, handlerTag, hf);
                }
            }

            @Override
            public void failure(RetrofitError retrofitError) {
                RetrofitNetworkService.handleError(handler, handlerTag, retrofitError);
            }
        });
    }

    /**
     * Register the new user synchronized
     * @param restAdapter   adapter for accessing the remote rest service
     * @param newUser       the user to be registered
     */
    public void register(RestAdapter restAdapter, HeroInfo newUser){

    }

    public void register(RestAdapter restAdapter, final HeroInfo heroInfo, final String clientId, final Handler handler, final Integer handlerTag){
        WalnutRegAuthRestfulService service = restAdapter.create(WalnutRegAuthRestfulService.class);
        service.register(clientId, heroInfo.alias, heroInfo.mobile, heroInfo.email, heroInfo.password, new Callback<HeroInfoRestfulWrapper>() {
            @Override
            public void success(HeroInfoRestfulWrapper result, Response response) {

                if (result != null && result.getUser() != null) {
                    RetrofitNetworkService.handleSuccess(handler, handlerTag, result.getUser());
                } else {
                    RetrofitNetworkService.handleError(handler,handlerTag, null);
                }

                //hero.userName = heroInfo.userName;
                //hero.password = heroInfo.password;
            }

            @Override
            public void failure(RetrofitError retrofitError) {
                RetrofitNetworkService.handleError(handler,handlerTag, retrofitError);
            }
        });
    }

    /**
     * Authenticate the user synchronized by user name and password
     * @param restAdapter
     * @param heroInfo
     */
    public HeroInfo login(RestAdapter restAdapter, HeroInfo heroInfo){
        WalnutRegAuthRestfulService service = restAdapter.create(WalnutRegAuthRestfulService.class);
        HeroInfoRestfulWrapper heroWrapper = (HeroInfoRestfulWrapper)service.login(heroInfo.userName, heroInfo.password);
        if(null != heroWrapper) {
            String pwd = heroInfo.password;
            HeroInfo hero = heroWrapper.getUser();
            if(null != hero) {
                hero.password = heroInfo.password;
                return hero;
            }
        }
        return null;
    }

    /**
     * Authenticate the user async by user name and password
     * @param restAdapter
     * @param bundle
     * @param handler
     * @param handlerTag
     */
    public void login(RestAdapter restAdapter, Bundle bundle, final Handler handler, final Integer handlerTag){
        retrieveData(restAdapter, bundle, handler, handlerTag);
    }

    public void getCode(RestAdapter restAdapter, String mobile, final Handler handler, final Integer handlerTag) {
        WalnutRegAuthRestfulService service = restAdapter.create(WalnutRegAuthRestfulService.class);
        service.getCode(mobile, new Callback<NetBase>() {
            @Override
            public void success(NetBase netBase, Response response) {
                if (netBase != null && netBase.getResultCode() == 200) {
                    RetrofitNetworkService.handleSuccess(handler, handlerTag, "ok");
                } else {
                    RetrofitNetworkService.handleSuccess(handler, handlerTag, "no");
                }
            }

            @Override
            public void failure(RetrofitError error) {
                RetrofitNetworkService.handleError(handler, handlerTag, error);
            }
        });
    }
}

